package com.flyee.controller.tools.cartoon.model
{
	import com.flyee.controller.tools.cartoon.control.AppearanceLoadManager;
	import com.flyee.controller.tools.cartoon.control.CreateEquipObjManager;
	import com.flyee.controller.tools.cartoon.view.CartoonSprite;
	import com.flyee.utils.GCHleper;
	import com.flyee.utils.load.model.LoaderModel;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class EquipsObj extends EventDispatcher
	{
		public var shareDeport:String = null;
		public var type:Number = -1;
		public var direction:Number = -1;
		private var equipList:Array= new Array();
		private var myID:String = null;
		private var myXML:XML = null;
		public var mySprite:CartoonSprite = new CartoonSprite();
		public function EquipsObj(t:String = null,id:String = null)
		{
			if(t)
			{
				this.myID = id;
				
				var xml:XML = new XML(t);
				this.myXML = xml;
				this.shareDeport = xml.shareDeport;
				this.type = xml.type;
				this.direction = xml.direction;
				
				if(mySprite == null)
				{
					mySprite = new CartoonSprite();
				}
				this.mySprite.myDir = this.direction;
				this.mySprite.myType = this.type;
				this.mySprite.x = 0;
				this.mySprite.y = 0;
				AppearanceLoadManager.getInstance().regist(this.shareDeport,this.getLoader,this.myID);
				
			}
		}
		private function getLoader(loader:LoaderModel):void
		{
			var tempLength:uint = this.myXML.equip.length()
			for(var i:int = 0;i< tempLength;i++)
			{
				var temp:EquipObj = new EquipObj();
				
				//生成MODEL
				var tempModel:CreateEquipObjModel = new CreateEquipObjModel();
				tempModel.type = this.type;
				tempModel.dir = this.direction;
				tempModel.part = this.myXML.equip[i].part.toString();
				tempModel.loadURL = this.shareDeport;
				tempModel.mySwfURL = this.myXML.equip[i].shareDeport.toString();
				if(this.myXML.equip[i].hasOwnProperty("color"))
				{
					tempModel.color = this.myXML.equip[i].color.toString();
				}
				tempModel.myID = this.myID;
				//注册到创建管理器中
				CreateEquipObjManager.getInstance().createEquipObj(tempModel,completeHandle,temp,errorHandle);
	
				equipList.push(temp);
			}
		}
		private function completeHandle(t:EquipObj):void
		{
			var temp:EquipObj = t;
			this.mySprite.addMyChild(temp);
			if(this.mySprite.myChildList.length == this.equipList.length)
			{
				this.dispatchEvent(new Event("createComplete"))
			}
		}
		private function errorHandle(t:EquipObj):void
		{
			var tempLength:uint = equipList.length;
			for(var i:int = 0; i < tempLength; i++)
			{
				var temp:EquipObj = this.equipList[i];
				if(temp == t)
				{
					this.equipList.splice(i,1);
				}
			}
			if(this.mySprite.myChildList.length == this.equipList.length)
			{
				this.dispatchEvent(new Event("createComplete"))
			}
		}
		public function getEquipList():Array
		{
			return equipList;
		}
		public function getEquip(part:Number):EquipObj
		{
			var tempLength:uint = equipList.length;
			for(var i:int = 0; i < tempLength; i++)
			{
				var temp:EquipObj = this.equipList[i];
				if(temp.part == part)
				{
					return temp;
				}
			}
			return null;
		}
		public function getXML():String
		{
			var tempLength:uint = equipList.length;
			var tempStr:String = "<equips>";
			tempStr+="<shareDeport>"+this.shareDeport+"</shareDeport>";
			tempStr+="<type>"+this.type+"</type>";
			tempStr+="<direction>"+this.direction+"</direction>";
			for(var i:int = 0; i < tempLength; i++)
			{
				var temp:EquipObj = this.equipList[i];
				//保存部件
				if(temp.part != -1)
				{
					tempStr+="<equip><part>"+temp.part+"</part>";
					if(temp.mySwfURL.length > 1)
					{
						tempStr+="<mySwfURL>"+temp.mySwfURL+"</mySwfURL>";
					}
					if(temp.color != null)
					{
						tempStr+="<color>"+temp.color+"</color>";
					}
					tempStr+="</equip>";
				}
			}
			tempStr +="</equips>"
			return tempStr;
		}
		public function removeEquip(part:Number):void
		{
			var tempLength:uint = equipList.length;
			for(var i:int = 0;i< tempLength;i++)
			{
				var temp:EquipObj = this.equipList[i];
				if(temp.part == part)
				{
					this.equipList.splice(i,1);
					this.mySprite.removeMyChild(temp);
					return
				}
			}
		}
		public function clear():void
		{
			var tempLength:uint = equipList.length;
			for(var i:int = 0;i< tempLength;i++)
			{
				var temp:EquipObj = this.equipList[i];
				temp.clear();
			}
			this.equipList.splice(0);
			this.mySprite.clear();
			GCHleper.clearAll(this.mySprite);
			this.mySprite = null;
		}
		public function addEquip(part:Number,p:EquipObj):void
		{
			removeEquip(part)
			this.equipList.push(p);
			this.mySprite.addMyChild(p);
		}
	}
}